function setKeyword() {
	var searchTypeID = $('#searchTypeID').val();
	
    var keyword = document.getElementById("keywordInput").value;
    keyword = keyword.replace(">", "").replace("<", "").replace("\"", "").replace("'", "").replace("-", "");
    document.getElementById("keyword").value = keyword;
	if (keyword == "") {
		document.getElementById("keyword").value = 0;
		return;
	}
	
	if (searchTypeID == 'supplier') {
		 supplierSearch();
	} else {
		keywordSearch();
	}
}

function setKeyword2() {
    var keyword = document.getElementById("keywordInput2").value;
    keyword = keyword.replace(">", "").replace("<", "").replace("\"", "").replace("'", "").replace("-", "");
    document.getElementById("keyword").value = keyword;
	if (keyword == "") {
		document.getElementById("keyword").value = 0;
		return;
	}
	
	detailSearch();
}

function setPriceArea() {
	var fromPrice = document.getElementById("fromPrice").value;
	var toPrice = document.getElementById("toPrice").value;
	var priceAreaID = "0";
	if (!fromPrice == '' || !toPrice == '') {
		priceAreaID = "";
		if (!fromPrice == '') {
			try {
				var fromPriceInt = parseInt(fromPrice);
			} catch(e) {
				alert('请输入整数');
				return;
			}
			priceAreaID += fromPrice;
		}
		priceAreaID += ",";
		if (!toPrice == '') {
			try {
				var toPriceInt = parseInt(toPrice);
			} catch(e) {
				alert('请输入整数');
				return;
			}
			priceAreaID += toPrice;
		}
		
	    document.getElementById("priceAreaID").value = priceAreaID;
		
		detailSearch();
	}
}

function keywordSearch() {
 	document.getElementById("bigTypeID").value = 0;
    document.getElementById("smallTypeID").value = 0;
    document.getElementById("tinyTypeID").value = 0;
    document.getElementById("siteProductTypeID").value = 0;
    document.getElementById("brandID").value = 0;
    document.getElementById("props").value = 0;
    document.getElementById("tag").value = 0;
    document.getElementById("priceAreaID").value = 0;
    document.getElementById("sortTypeID").value = 0;
    document.getElementById("pageIndex").value = 1;
    document.getElementById("showType").value = 0;
    detailSearch();
}

function supplierSearch() {
	var keyword = document.getElementById("keyword").value;
    if (keyword == "") keyword = 0;
    
    var pageIndex = "1";
    
    var url = encodeURI("/supplier/list-0-0-" + keyword +  "-0-0-0-0-" + pageIndex + ".html");
    location.href = url;
}

function detailSearch() {
    var bigTypeID = document.getElementById("bigTypeID").value;
    if (bigTypeID == "") bigTypeID = 999;
    
    var smallTypeID = document.getElementById("smallTypeID").value;
    if (smallTypeID == "") smallTypeID = 0;
    
    var tinyTypeID = document.getElementById("tinyTypeID").value;
    if (tinyTypeID == "") tinyTypeID = 0;
    
    var siteProductTypeID = document.getElementById("siteProductTypeID").value;
    if (siteProductTypeID == "") siteProductTypeID = 0;
    
    var brandID = document.getElementById("brandID").value;
    if (brandID == "") brandID = 0;
    
    var props = document.getElementById("props").value;
    if (props == "") props = 0;
    
    var tag = document.getElementById("tag").value;
    if (tag == "") tag = 0;
    
    var keyword = document.getElementById("keyword").value;
    if (keyword == "") keyword = 0;
    
	if(document.getElementById("fromPrice") && document.getElementById("fromPrice").value == "" && document.getElementById("toPrice") && document.getElementById("toPrice").value == "") {
	    document.getElementById("priceAreaID").value = "0";
	} 
    
    var priceAreaID = document.getElementById("priceAreaID").value;
    if (priceAreaID == "") priceAreaID = 0;
    
    var sortTypeID = document.getElementById("sortTypeID").value;
    if (sortTypeID == "") sortTypeID = 0;
    
//    var pageIndex = document.getElementById("pageIndex").value;
//    if (pageIndex == "") pageIndex = 0;
    var pageIndex = "1";
    
    var showType = document.getElementById("showType").value;
    if (showType == "") showType = 0;
    
    var product_stock = $("#sto").is(":checked") ? "1" : "0";
    if (product_stock == "") product_stock = 0;
    
    var module = document.getElementById("module").value;
    var action = document.getElementById("action").value;
    
    var supplierID = document.getElementById("supplierID").value;
    if (supplierID == "") supplierID = 0;
    var listAction = "product/list-";
    if(module == "supplier" && action != "list") {
    	listAction = module + "/productlist-";
    }
    
    var url = encodeURI("/" + listAction + bigTypeID + "-" + smallTypeID + "-" + tinyTypeID + "-" + brandID + "-"  + keyword +  "-" + props + "-" + tag + "-" + priceAreaID + "-" + sortTypeID + "-"  + showType + "-" + pageIndex + "-" + siteProductTypeID + "-0-0-0-0-0-" + supplierID + "-0-" + product_stock + ".html");
    location.href = url;
}

function setSortType(sortTypeID) {
	document.getElementById('pageIndex').value = '1';
	document.getElementById('sortTypeID').value = sortTypeID;
	detailSearch();
}

function Cnt_UpDown(key, element) {
   var obj = document.getElementById(element);
   if(isNaN(parseInt(obj.value))) { 
      obj.value = 1;
      return;
   }
   else {
      if(key == "D") {
   
         if(parseInt(obj.value) <= 1) {
            return;
         }
         else {
            obj.value = parseInt(obj.value) - 1;
            return;
         }
      }
      else {
       	 if (parseInt(obj.value) == 999) {
       	 	return ;
       	 } 
       	 else {
       	 	obj.value = parseInt(obj.value) + 1;
         	return;
       	 }

      }
   }
}

function changeAutoLoginFlagFlag() {
	var remeberFlag = document.getElementById('autoLoginFlag');
	if (remeberFlag.checked) {
		$('#autoLoginFlag').val('1');
	} else {
		$('#autoLoginFlag').val('');
	}
}	

function calendar(object) {
    WdatePicker({onpicking:function () {
        object.focus();
    }, skin:"whyGreen", minDate:""});
}


function jsonpCallback(result) {
	var resultData = result.data;
    var gIsCheck = result.ischeck;
    if(result.status == '400') {
    	$("#errorTips").show();
    	$("#queryContext").hide();
    	return;
    }
    if(typeof(resultData) == "undefined") {
    	$("#errorTips").show();
    	return;
    }
    var resultTable = $("#queryResult");
    resultTable.empty();
    for (var i = resultData.length - 1; i >= 0; i--) {
        var className = "";
        if (resultData.length == 1 && gIsCheck == 0) {
            className = "status status-wait";
        } else {
            if (resultData.length == 1 && gIsCheck == 1) {
                className = "status status-check";
            } else {
                if (i == 0 && gIsCheck == 0) {
                    className = "status status-wait";
                } else {
                    if (i == 0 && gIsCheck == 1) {
                        className = "status status-check";
                    } else {
                        if (i == resultData.length - 1) {
                            className = "status status-first"
                        } else {
                            className = "status"
                        }
                    }
                }
            }
        }
        var context = resultData[i].context;
        context = getJumpNetContext(context, gCompanyCode, "fonter2");
        if (i == 0) {
            resultTable.append('<tr class="last"><td class="row1">' + resultData[i].ftime + '</td><td class="' + className + '">&nbsp;</td><td>' + context + "</td></tr>")
        } else {
            resultTable.append('<tr><td class="row1">' + resultData[i].ftime + '</td><td class="' + className + '">&nbsp;</td><td>' + context + "</td></tr>")
        }
    }
    $("#queryContext").show().scrollTop(1000);
}

function getJumpNetContext(e, c, a) {
	var d = "(?:(?!的|员|型|是).|^)";
	var b = ".?到达.?|.?问题.?|.?派件.?|.?签收.?|.?疑难.?|.?扫描.?|.?装袋.?|.?装包.?|.?妥投.?|.?操作员.?|.?审核.?|.?备注.?|.?客服.?|.?网点经理.?|.?员工.?|.?门卫.?|.?本人.?|.?草签.?|.?图片.?|.?分拨中心.?";
	var h = {
		shentong: "5",
		huitongkuaidi: "6",
		huiqiangkuaidi: "27",
		tiantian: "7",
		zhaijisong: "12",
		quanfengkuaidi: "23",
		longbanwuliu: "24",
		guotongkuaidi: "20",
		kuaijiesudi: "18",
		debangwuliu: "1",
		zhongtong: "3",
		yunda: "2"
	};
	switch (c) {
	case("shentong"):
	case("huitongkuaidi"):
	case("huiqiangkuaidi"):
	case("tiantian"):
	case("quanfengkuaidi"):
	case("longbanwuliu"):
	case("guotongkuaidi"):
	case("kuaijiesudi"):
		var g = d + "【((?:(?!" + b + ")[^\\s\\d【]){2,})】";
		var f = new RegExp(g, "gi");
		e = e.replace(f,
		function(j, i, k) {
			return '【<a href="http://www.kuaidi100.com/network.jsp?from=' + a + "&searchText=" + encodeURIComponent(i) + "&company=" + h[c] + '" target="_blank">' + i + "</a>】"
		});
		break;
	case ("debangwuliu"):
	case ("zhaijisong"):
	case ("zhongtong"):
		var g = d + "\\[((?:(?!" + b + ")[^\\s\\d【]){2,})\\]";
		var f = new RegExp(g, "gi");
		e = e.replace(f,
		function(j, i, k) {
			return '[<a href="http://www.kuaidi100.com/network.jsp?from=' + a + "&searchText=" + encodeURIComponent(i) + "&company=" + h[c] + '" target="_blank">' + i + "</a>]"
		});
		break;
	case ("yunda"):
		var g = "((?:(?!" + b + ")\\S){2,}):";
		var f = new RegExp(g, "gi");
		e = e.replace(f,
		function(j, i, k) {
			return '<a href="http://www.kuaidi100.com/network.jsp?from=' + a + "&searchText=" + encodeURIComponent(i) + "&company=" + h[c] + '" target="_blank">' + i + "</a>:"
		});
		break
	}
	return e
}
/**
 * 快递100 JSONP
 * @param com
 * @param postid
 * @return
 * @author luganli
 */
var gCompanyCode = ""
function searchWuLiu(com, postid) {
    var JSONP = document.createElement("script");  
    JSONP.type="text/javascript";  
    JSONP.src="http://www.kuaidi100.com/query?type=" + com + "&postid=" + postid + "&callback=jsonpCallback";  
    document.getElementsByTagName("head")[0].appendChild(JSONP);
    gCompanyCode = com;
}


function selectCity() {
	refreshItem('fastOrder', 'refreshCitySelect', 'citySelect');
}

function selectCity2(addressIndex) {
	document.getElementById('addressIndex').value = addressIndex;
	refreshItem('fastOrder', 'refreshCitySelectInAddress', 'citySelect' + addressIndex);
}